define([
	'../libs/zepto',
	'../common/base',
	'../common/ajax',
	'../common/yingSoftPrg',
	'../common/yingSoftCookie',
	'../common/storage',
	'../common/agentInfo',
	'../common/dialog',
	'../common/api'
], function() {
	this.init = function() {
		select_tizhuan(); //选取支付方式
		getPriceList(); //获得价格
		_getAppEName();
	}
	var dd=0;
	//选取支付方式
	function select_tizhuan() {
		//获取当前所选科目
		var curLesson = '<div class="curBox"><p>当前科目</p><span class="curBook">' + G_Cookie.getAppName() + '</span></div>'
		$('.a').html(curLesson);
		//卡片类型
		$(document).on('click', '.select_pay li', function() {
			$(this).addClass('checked').siblings().removeClass('checked');
		});
		//支付方式
		$(document).on('click', '.select_way .wayPay', function() {
			$(this).addClass('checked').siblings().removeClass('checked');

		});
		//确认购买，选择跳转路径
		$(document).on('click', '.payBtn', function() {
			$('#loadingDiv').css('display','block')
			_getOrderID();
		})
	};
	//获得价格
	function getPriceList() {
		//获取英文名
		var getAppEName = G_Cookie.getAppEName();
		var url = '/api/price/getPriceList/' + getAppEName;
		var success = function(json) {
			var jsonData = JSON.parse(json);
			dd=jsonData[0].AppClassID;
			var card = [];
			for(var i = 0; i < jsonData.length; i++) {
				var seasonCardPrice = jsonData[i].SeasonCardPrice;
				var seasonCardDays = jsonData[i].SeasonCardDays;
				var mounthCardPrice = jsonData[i].MonthCardPrice;
				var mounthCardDays = jsonData[i].MonthCardDays;
				var yearCardPrice = jsonData[i].YearCardPrice;
				var yearCardDays = jsonData[i].YearCardDays;
				var showSeasonPrice = (parseFloat(seasonCardPrice) / 92).toFixed(2);
				var showYearPrice = (parseFloat(yearCardPrice) / 366).toFixed(2);
				var showMounthPrice = (parseFloat(mounthCardPrice) / 366).toFixed(2);

				if(mounthCardDays) {
					cardPrice = mounthCardPrice;
					if(cardPrice !== 0) {
						//card.push('1个月（月卡）_' + cardPrice + '_' + showMounthPrice);
					}

				}
				if(seasonCardDays) {
					cardPrice = seasonCardPrice;
					if(cardPrice !== 0) {
						card.push('3个月（季卡）_' + cardPrice + '_' + showSeasonPrice);
					}

				}
				if(yearCardDays) {
					cardPrice = yearCardPrice;
					card.push('12个月（年卡）_' + cardPrice + '_' + showYearPrice);
				}
			}
			var html = " ";
			for(var i = 0; i < card.length; i++) {
				html += '<li class="mprice"><h2>￥' + card[i].split('_')[1] + '</h2><h3>' + card[i].split('_')[0] + '</h3><h4>日均￥' + card[i].split('_')[2] + '</h4></li>';
			}
			var html0 = '<p>选择充值</p><ul class="payList"></ul>'
			var html1 = '<p>选择支付</p><ul class="wayList"><li class = "wayPay checked" value="1"><div><img src="/images/alipay.png" alt=""><span class="ali" >支付宝支付</span></div></li><li class= "wayPay" value="3"><div><img src="/images/wechat.png" alt=""><span class="ali" appid = "' + jsonData[0].AppClassID + '">微信支付</span></div></li></ul>';
			var html2 = '<div class="payBtn">立即支付</div>';
			$(".select_pay").append(html0);
			$('.payList').append(html);
			$(".mprice").eq(0).addClass('checked');
			$('.select_way').append(html1);
			$('.b').append(html2);
			return;

			G_Prg.alert('价格获取失败');
		};
		G_AjaxApi_Record.get(url, true, success);

	}

	//截取url中的英文名字
	function _getAppEName() {
		var appEName = G_Prg.getQueryString('appEName');
		if(!appEName || appEName.toString().trim() === '') {
			appEName = G_Cookie.getAppEName();
		}
		return appEName;
	}
	//浏览器类型
	function _getClientType() {
		var browser = {
			versions: function() {
					var u = navigator.userAgent,
						app = navigator.appVersion;
					return {
						ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
						android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
						iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
						iPad: u.indexOf('iPad') > -1, //是否iPad
						webApp: u.indexOf('Safari') == -1 //是否web应该程序，没有头部与底部
					};
				}
				()
		};
		if(browser.versions.iPhone || browser.versions.iPad) {
			return 'web_iPhone';
		}
		if(browser.versions.ios || browser.versions.android || browser.versions.webApp) {
			return 'web_android';
		}
		return 'web_pc';
	}

	function _getUserAgent() {
		var userAgent = navigator.userAgent.split('/');
		return userAgent;
	}

	function _getOrderID() {
		//获取卡片类型和支付方式
		var priceType = 0;
		var appcClassID = 0;
		var payType = 0;
		//声明价格的type并记录用来传给后台
		if($('.payList li.checked')) {
			if($('.payList li.checked h3').text() == "12个月（年卡）") {
				priceType = 3;
			} else {
				priceType = 2;
			}
		}
		if($('.wayList li.checked')) {
			payType = $('.wayList li.checked').attr('value');

		}

		var data = {
			userID: G_Cookie.getUserID(),
			userName: G_Cookie.getUserName(),
			softName: G_Cookie.getAppName(),
			softNameEname: _getAppEName(),
			cookie: G_Cookie.getGuid(),
			seriesID: dd,
			type: priceType,
			equipment: _getClientType(),
			AgentNo: G_AgentInfo.getAgentCode(),
			AgentSite: 'http://yxgjp.ksbao.com/'
		}

		var url = '/api/price/getOrderNumber/';
		var success = function(json) {
			var jsonData = JSON.parse(json);
			if(jsonData.status == 200) {
				var orderID = jsonData.orderID;
				var alipay = jsonData.alipay;
				var userID = G_Cookie.getUserID();
				if(payType === '3') {
					if(_getClientType() == "web_android") {
						//微信扫码支付
						//window.location = '/wxpaysm?softName=' + escape(G_Cookie.getAppName()) + '&appClassID=' + dd + '&selectType=' + priceType + '&appEName=' + G_Cookie.getAppEName();
						var data = {
							userID: G_Cookie.getUserID(),
							userName: G_Cookie.getUserName(),
							softName: G_Cookie.getAppName(),
							softNameEname: _getAppEName(),
							cookie: G_Cookie.getGuid(),
							seriesID: dd,
							type: priceType,
							equipment: _getClientType(),
							AgentNo: 9001,
							AgentSite: 'http://yxgjp.ksbao.com/'
						}
						_weChatPayment(data);
					} else {
						//微信安卓封壳支付
						
					}

				} else
				if(payType === '1') {
					//手机网页支付宝支付
					_wapAlipay(orderID, alipay);
				} else {
					G_Prg.alert('没有选择支付方式');
				}
				return;
			} else {
				//console.log(jsonData.status);
			}
			G_Prg.alert(jsonData.msg);
		};
		G_AjaxApi_Record.post(url, data, true, success);
	}

	//手机支付宝跳转
	function _wapAlipay(orderID, alipay) {
		var url = '/api/price/paySubmit/' + orderID + '/phone/' + alipay;
		var success = function(json) {
			var jsonData = JSON.parse(json);
			if(jsonData.status === 200) {
				var url = jsonData.data;
				window.location.href = url;
				return;
			}
			G_Prg.alert(jsonData.msg);
		};
		G_AjaxApi_Record.get(url, true, success);
	}
	//微信封壳函数支付跳转
	function _weChatPayment(data) {
		
		var url = '/api/price/getOrderNumber/';
		var success = function(json) {

			var jsonData = JSON.parse(json);
			if(jsonData.status === 200) {

				var orderID = jsonData.orderID;
				var json = {
						"appID": G_Cookie.getAppID(),
						"appEName": G_Cookie.getAppEName(),
						"dayType": data.type,
						"userID": G_Cookie.getUserID(),
						"userName": G_Cookie.getUserName(),
						"appName": G_Cookie.getAppName(),
						"orderID": orderID,
						//"prepay_id": jsonData.prepay_id
						"price": jsonData.price,
						"now": G_Prg.datetimeFormat(new Date(), 'yyyy-MM-dd hh:mm:ss')
					}
					//				var now = G_Prg.datetimeFormat(new Date(), 'yyyy-MM-dd hh:mm:ss');
					//				console.log(now)
//				console.log(json)
				window.android.getParams(JSON.stringify(json));
				return;
			}
		}
		G_AjaxApi_Record.post(url, data, false, success);
	};

	init();
});